﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ch01
{
    public partial class FrmDataGridView2 : Form
    {
        public FrmDataGridView2()
        {
            InitializeComponent();
        }

        private void FrmDataGridView2_Load(object sender, EventArgs e)
        {
            BoinComdoBox();
            this.dataGridView1.AutoGenerateColumns = false;
            BoinDataGridView();
        }

        public void BoinComdoBox()
        {
            string sql ="select * from grade";
            DataTable dt = DBHelper.SelectTable(sql);
            DataRow dr = dt.NewRow();
            dr["no"] = 0;
            dr["gradename"] = "全部";
            dt.Rows.InsertAt(dr,0);
            this.comboBox1.DisplayMember = "gradename";
            this.comboBox1.ValueMember = "no";
            this.comboBox1.DataSource = dt;
        }
        public void BoinDataGridView()
        {
            string sql = "select s.*,g.gradename from student s,grade g where s.gradeid=g.no and 1=1";
            string name = this.textBox1.Text;
            if (!string.IsNullOrEmpty(name))
            {
                sql += string.Format(" and name like '%{0}%'",name);
            }
            int gradeid = Convert.ToInt32(this.comboBox1.SelectedValue);
            if (gradeid!=0)
            {
                sql += " and s.gradeid="+gradeid;
            }
            this.dataGridView1.DataSource = DBHelper.SelectTable(sql);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BoinDataGridView();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count<=0)
            {
                MessageBox.Show("至少选择一条数据信息！");
                return;
            }
            object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
            int sutNo = Convert.ToInt32(o);
            string sql = "delete from student  where sutNo="+sutNo;
            int result = DBHelper.DataUpdate(sql);
            if (result > 0)
            {
                MessageBox.Show("删除成功！");
                BoinDataGridView();
            }
            else
            {
                MessageBox.Show("删除失败！");
            }
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count<=0)
            {
                MessageBox.Show("至少选择一条数据信息！");
                return;
            }
            object o = this.dataGridView1.SelectedRows[0].Cells[0].Value;
            int sutNo = Convert.ToInt32(o);
            FrmUpdateSutInfo fu = new FrmUpdateSutInfo();
            fu.sutNo = sutNo;
            fu.ShowDialog();
            BoinDataGridView();
        }

        
    }
}
